package com.bkgy.modular.tbdistributionopt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tbdecsionrule.entity.TbDecsionRule;
import com.bkgy.modular.tbdistributionopt.dao.TbDistributionOptDao;
import com.bkgy.modular.tbdistributionopt.entity.TbDistributionOpt;
import com.bkgy.modular.tbdistributionopt.service.TbDistributionOptService;
import com.bkgy.sys.common.entity.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 13:14 2025/10/23
 */
@Service
public class TbDistributionOptServiceImpl extends ServiceImpl<TbDistributionOptDao, TbDistributionOpt> implements TbDistributionOptService {

    @Autowired
    private TbDistributionOptDao tbDistributionOptDao;

    @Override
    public ResultData findDataTbDistributionOptByPage(String data) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageIndex = jsonObject.getInteger("pageIndex");
            Integer pageSize = jsonObject.getInteger("pageSize");
            //查询条件暂无，后续需要再根据需求添加
            JSONObject condition = jsonObject.getJSONObject("condition");
            String steelGrade = condition.getString("steelGrade");
            LambdaQueryWrapper<TbDistributionOpt> queryWrapper = new LambdaQueryWrapper<>();

            if (!StringUtils.isEmpty(steelGrade)) {
                queryWrapper.eq(TbDistributionOpt::getSteelGrade,steelGrade);
            }
            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            Page<TbDistributionOpt> page = new Page<>(pageIndex,pageSize);
            Page<TbDistributionOpt> tbDistributionOptPage = this.page(page, queryWrapper);
            return ResultData.ResultDataSuccess(tbDistributionOptPage.getRecords(), (int) tbDistributionOptPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }


    @Override
    public ResultData insertOne(String data) {
        try{
            TbDistributionOpt tbDistributionOpt = JSON.parseObject(data,TbDistributionOpt.class);
            this.save(tbDistributionOpt);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    }


    @Override
    public ResultData deleteOneById(String data) {
        try{
            TbDistributionOpt tbDistributionOpt = JSON.parseObject(data,TbDistributionOpt.class);
            this.removeById(tbDistributionOpt.getId());
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData deleteManyByIds(String data) {
        try {
            List<Integer> list = JSON.parseArray(data, Integer.class);
            this.removeByIds(list);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData updateDataTbDistributionOpt(JSONObject jsonObject) {
        try {
            String id = jsonObject.getString("id");
            LambdaQueryWrapper<TbDistributionOpt> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbDistributionOpt::getId,id);
            String data = jsonObject.getJSONObject("data").toJSONString();
            TbDistributionOpt tbDistributionOpt = JSON.parseObject(data,TbDistributionOpt.class);
            tbDistributionOptDao.update(tbDistributionOpt,queryWrapper);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败，错误信息为" + e.getMessage(), null);
        }
    }


}
